package yagwl.client.model.fnd;

import java.util.List;

import yagwl.client.model.MasterDetailDTO;
import yagwl.client.model.MasterDetailListCache;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class FlexEntityListCache extends MasterDetailListCache {

  private FndRpcServiceAsync fndService = GWT
      .create(FndRpcService.class);

  @Override
  public void fetch() {
    if (isLoadInProgress())
      return;
    loadStart();

    fndService.getFlexEntities(new AsyncCallback<List<MasterDetailDTO>>() {
      @Override
      public void onFailure(Throwable caught) {
        onLoadFailure(caught);
      }

      @Override
      public void onSuccess(List<MasterDetailDTO> result) {
        onLoadSuccess(result);
      }
    });
  }

  @Override
  protected void masterItemCreate(final MasterDetailDTO entity) {
    fndService.createFlexEntity(entity, 
        new AsyncCallback<RpcStatus>() {
          @Override
          public void onFailure(Throwable caught) {
            Window.alert(caught.toString());
          }

          @Override
          public void onSuccess(RpcStatus result) {
            masterItemCreated(entity, result);
          }
        });
  }

  /*
  @Override
  protected void nodeCreate(final TreeNodeDTO parentNode,
      final TreeNodeDTO newNode) {
    productService.createCategory(newNode, new AsyncCallback<RpcStatus>() {
      @Override
      public void onFailure(Throwable caught) {
        Window.alert(caught.toString());
      }

      @Override
      public void onSuccess(RpcStatus result) {
        nodeCreated(parentNode, newNode, result);
      }
    });
  }

  @Override
  protected void nodeDelete(final TreeNodeDTO treeNode) {
    productService.deleteCategorySubTree(treeNode.getId(),
        new AsyncCallback<RpcStatus>() {
          @Override
          public void onFailure(Throwable caught) {
            Window.alert(caught.toString());
          }

          @Override
          public void onSuccess(RpcStatus result) {
            nodeDeleted(treeNode, result);
          }
        });
  }

  @Override
  public void nodeUpdate(final TreeNodeDTO origNode,
      final TreeNodeDTO updatedNodeProxy) {
    productService.updateCategory((TreeNodeDTO) updatedNodeProxy,
        new AsyncCallback<RpcStatus>() {
          @Override
          public void onFailure(Throwable caught) {
            Window.alert(caught.toString());
          }

          @Override
          public void onSuccess(RpcStatus result) {
            origNode.setLabel((String) updatedNodeProxy.get("name"));
            nodeUpdated(origNode, updatedNodeProxy, result);
          }
        });
  }

  @Override
  public void nodeLabelUpdate(final TreeNodeDTO origNode,
      final TreeNodeDTO updatedNodeProxy) {
    updatedNodeProxy.put("name", updatedNodeProxy.getLabel());
    productService.updateCategory((TreeNodeDTO) updatedNodeProxy,
        new AsyncCallback<RpcStatus>() {
          @Override
          public void onFailure(Throwable caught) {
            Window.alert(caught.toString());
          }

          @Override
          public void onSuccess(RpcStatus result) {
            origNode.put("name", updatedNodeProxy.getLabel());
            nodeLabelUpdated(origNode, updatedNodeProxy, result);
          }
        });
  }

  @Override
  public void itemAdd(final TreeNodeDTO parentNode, final ItemDTO item) {
    productService.addProduct(parentNode.getId(), item.getId(),
        new AsyncCallback<RpcStatus>() {
          @Override
          public void onFailure(Throwable caught) {
            Window.alert(caught.toString());
          }

          @Override
          public void onSuccess(RpcStatus result) {
            itemAdded(parentNode, item, result);
          }
        });
  }

  @Override
  public void itemRemove(final TreeNodeDTO parentNode, final ItemDTO item) {
    productService.removeProduct(parentNode.getId(), item.getId(),
        new AsyncCallback<RpcStatus>() {

          @Override
          public void onFailure(Throwable caught) {
            Window.alert(caught.toString());
          }

          @Override
          public void onSuccess(RpcStatus result) {
            itemRemoved(parentNode, item, result);
          }
        });
  }
  */
}
